﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL.DAO;
using COM.DTO;
using log4net.Repository.Hierarchy;
using COM;

namespace BUS
{
    public class UserHandler
    {
        public static bool CreateNewUser(COM.DTO.User user)
        {
            try
            {
                user.UserId = Common.IDGenerator(CONST.PREFIX.USER);
                UserDAO.Add(user);
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
                return false;
            }

        }
        public static Role getRole(byte roleId)
        {
            return RoleDAO.GetRoleById(roleId);
        }
        public static User Login(string username, string password)
        {
            try
            {
                User currentUser = DAL.DAO.UserDAO.GetUserByUsername(username);
                if (currentUser == null)
                {
                    return null;
                }
                else
                {
                    if (!COM.Encrypt.EncodePassword(password).Equals(currentUser.Password) || currentUser.Role.RoleId > COM.CONST.ROLE.HOTELOWNER)
                    {
                        return null;
                    }
                    else
                    {
                        return currentUser;
                    }
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }
    }
}
